using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ListaFacil
{
    public partial class frmCadastroSecaoTelUtil : Form
    {
        private DataTable dtSecao;
        private BindingSource bsSecao;
        private bool editMode = false;
        public frmCadastroSecaoTelUtil()
        {
            InitializeComponent();
            inicializar();
        }
        private void HabilitaBotoes(bool flagHabilita)
        {
            bindingNavigatorAddNewItem.Enabled = flagHabilita;
            bindingNavigatorDeleteItem.Enabled = flagHabilita;
            bindingNavigatorMoveFirstItem.Enabled = flagHabilita;
            bindingNavigatorMoveLastItem.Enabled = flagHabilita;
            bindingNavigatorMoveNextItem.Enabled = flagHabilita;
            bindingNavigatorMovePreviousItem.Enabled = flagHabilita;
            bindingNavigatorPositionItem.Enabled = flagHabilita;
            
            EdittoolStripButton.Enabled = flagHabilita;
            saveToolStripButton.Enabled = !flagHabilita;
            canceltoolStripButton.Enabled = !flagHabilita;
            txtnome.Enabled = !flagHabilita;           
            dataGridView1.Enabled = flagHabilita;
        }

        private void inicializar()
        {
            dtSecao = Program.SLF.consultarSecoesTelUtilCadastradas();
            bsSecao = new BindingSource();
            bsSecao.DataSource = dtSecao;
            bindingNavigator1.BindingSource = bsSecao;
            dataGridView1.DataSource = bsSecao;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            HabilitaBotoes(true);

        }

       

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
        }

        private void Navegacao(object sender, EventArgs e)
        {
            this.editMode = false;
        }

        private void Edicao(object sender, EventArgs e)
        {
            this.editMode = true;
        }
        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }
        private void txtLogradouro_TextChanged(object sender, EventArgs e)
        {
            if (this.editMode)
                this.HabilitaBotoes(false);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string retorno = "";
            int numeroregistro = 0;
            int linha = 0;

            if (this.editMode == false)
            {
                retorno = Program.SLF.inserirSecaoTelUtil(txtnome.Text.Trim());
                
                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) - 1;
                numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells[0].Value);
                retorno = Program.SLF.updateSecaoTelUtil(txtnome.Text.Trim(), Int32.Parse(lblid.Text));

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            inicializar();
            this.HabilitaBotoes(true);
        }
        private void dgvLogradouro_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                lblid.Text = dataGridView1.CurrentRow.Cells["id_secao"].Value.ToString();
                txtnome.Text = dataGridView1.CurrentRow.Cells["nome"].Value.ToString();
                
                this.Navegacao(this, e);
            }
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            string retorno = "";

            retorno = Program.SLF.removerSecaoTelUtil(Convert.ToInt32(this.lblid.Text));
            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);

            inicializar();
        }

        private void EdittoolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
            this.editMode = true;
        }

        private void canceltoolStripButton_Click(object sender, EventArgs e)
        {
            
            this.HabilitaBotoes(true);
        }

        private void txtnome_TextChanged(object sender, EventArgs e)
        {

            if (this.editMode)
                this.HabilitaBotoes(false);
        }

        private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string retorno = "";

            retorno = Program.SLF.removerSecaoTelUtil(Convert.ToInt32(this.lblid.Text));
            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);

            inicializar();
            HabilitaBotoes(true);
        }
       

        
    }
}